﻿using UnityEngine;
using System.Collections;

public class cam : MonoBehaviour {

	public GameObject target;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {

		if(target!=null)
		{
			float z=transform.position.z;
			transform.position -= (transform.position-target.transform.position)*Time.deltaTime*10f;
			transform.position = new Vector3(transform.position.x,transform.position.y,z);

			camera.orthographicSize-=(camera.orthographicSize-(target.transform.localScale.x*3f))*Time.deltaTime/30;
		}


		if(camera.orthographicSize>2 || Input.GetKeyDown("space"))
		{
			object[] allObjects = FindObjectsOfTypeAll(typeof(GameObject)) ;
			foreach(object obj in allObjects)
			{
				GameObject g = (GameObject) obj;

				//if (((GameObject) thisObject).activeInHierarchy)
				{

					g.transform.position=new Vector3(g.transform.position.x/2,g.transform.position.y/2,g.transform.position.z/2);
					g.transform.localScale=new Vector3(g.transform.localScale.x/2,g.transform.localScale.y/2,g.transform.localScale.z/2);

					if(g.rigidbody2D!=null)
					g.rigidbody2D.velocity=new Vector2(g.rigidbody2D.velocity.x/2,g.rigidbody2D.velocity.y/2);
				}
			}
			camera.orthographicSize/=2;
		}
	}

}
